package com.demo.crud.config;

import com.demo.crud.component.MyLocalResolver;
import com.demo.crud.interceptor.LoginHandlerInterceptor;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Created with IntelliJ IDEA.
 * Desc:MVC的扩展配置文件
 *
 * @author: keen
 * Date: 2019-06-04
 * Time: 23:24
 */
@Configuration
public class MyMvcConfig {

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("index");
                registry.addViewController("main.html").setViewName("dashboard");
            }

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new LoginHandlerInterceptor())
                        .addPathPatterns("/**")
                        .excludePathPatterns("/login", "/", "/index.html", "/asserts/**");
            }
        };
    }


    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocalResolver();
    }

    /*@Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6388");
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }*/

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
